//这个程序的目的是测试主机计算聚类中心的时间开销，会创建并填充一系列的向量以模拟聚类中心
//gcc compute_centro_distance.c min_list.c -lm -O3 -o compute_centro_distance

#include <stdio.h>
#include "time_printer.h"
#include <time.h>
#include <math.h>
#include "min_list.h"

void fill_array_float(float* array, int length){
	for(int i=0; i<length; i++){
        int randnum = (rand()+1)*943%624;
		array[i] = ((randnum*randnum)%1000)/1000.0;
	}
}

#define dis_type float
double EuclideanDistance(dis_type* pointA, dis_type* pointB, int dimen){
	double result = 0;
	dis_type temp = 0;
	for(int i=0; i<dimen; i++){
		temp = pointA[i]-pointB[i];
		temp = temp*temp;
		result += temp;
	}
	// result = sqrt(result);
	return result;
}

int main(){
    printf("Start Compute\n");
    srand((unsigned)time(0));

    //Fill centroid
    int center_count = 2000; //聚类中心的数量
    int feature_count = 128; //每个向量的特征数

    float center_vectors[center_count][feature_count]; //聚类中心
    float query_vector[feature_count]; //查询向量

    void* timer = init_record_time_printer("vector computer");

    for(int i=0; i<center_count; i++){
        fill_array_float(center_vectors[i], feature_count);
        // printf("%dth line: %f, %f, %f\n",i,center_vectors[i][0],center_vectors[i][1],center_vectors[i][2]);
    }
    fill_array_float(query_vector, feature_count);
    // printf("query: %f, %f, %f\n",query_vector[0],query_vector[1],query_vector[2]);

    record_time_printer(timer, "fill vector done");

    //计算距离和插入测试
    int K = 10;
    // void* minlist_mem = malloc(sizeof(struct minlist)+K*sizeof(struct minlist_entry));
    struct minlist* minlist = create_minlist(K);
    struct minlist_entry tmp_entry;

    double ret;
    for(int i=0; i<center_count; i++){
        double ret = EuclideanDistance(center_vectors[i],query_vector,feature_count);
        tmp_entry.id = i;
        tmp_entry.distance = ret;
        insert_minlist(minlist, &tmp_entry);
    }
    // printf("ret:%f\n",ret);

    record_last_time(timer);

    print_minlist(minlist);

    destory_minlist(minlist);

    return 0;
}